# D1 mini + 1.44" SPI TFT (ST7735 128×128) — Quick Start

This guide shows how to wire a 1.44" 128×128 ST7735 TFT to a LOLIN (WEMOS) D1 mini (ESP8266), set up Arduino IDE, and display a centered romantic quote using two different fonts. It also includes an optional section for showing color images from a microSD card.

---

## 1) Hardware
- **MCU:** LOLIN (WEMOS) **D1 mini** (ESP8266)
- **Display:** 1.44" **ST7735** TFT, 128×128 (GREENTAB) — often sold as MSP1443
- **Power:** VCC 5V (module is typically 5V tolerant), **LED/backlight at 3.3V**
- Jumper wires  
- *(Optional)* MicroSD module (to load BMP images from SD)

> Many 1.44" panels are ST7735, not ILI9163. Use `INITR_144GREENTAB` for 128×128 ST7735. If you see a bottom “noise bar” or partial fill in other drivers, switch to ST7735 with this init.

---

## 2) Arduino IDE Setup
1. **Add ESP8266 Boards URL**  
   *File → Preferences → Additional Boards Manager URLs*  
   `http://arduino.esp8266.com/stable/package_esp8266com_index.json`
2. **Select Board**  
   *Tools → Board → ESP8266 →* **LOLIN (WEMOS) D1 mini**
3. **Recommended Tool Settings**
   - Upload Speed: **921600** (use **115200** if uploads fail)
   - CPU Frequency: **80 MHz**
   - Flash Size: **4MB (FS: 1MB)** (or any 4MB option available)
   - Flash Mode: **DIO** (use DOUT only for certain clones)
   - Erase Flash: **Only Sketch**
   - Select the correct **COM/tty** port

---

## 3) Libraries (Library Manager)
- **Adafruit GFX Library**
- **Adafruit ST7735 and ST7789 Library**
- (Optional for images) **Adafruit ImageReader Library** and **SD**

---

## 4) Wiring (D1 mini → TFT)
| TFT Pin        | D1 mini Pin | Description      |
|----------------|-------------|------------------|
| **LED**        | **3.3V**    | Backlight (3.3V recommended) |
| **SCK**        | **D5**      | SPI Clock        |
| **SDA / MOSI** | **D7**      | SPI MOSI         |
| **A0 / DC**    | **D3**      | Data/Command     |
| **RESET**      | **D2**      | Reset            |
| **CS**         | **D1**      | Chip Select      |
| **GND**        | **G**       | Ground           |
| **VCC**        | **5V**      | Power (+5V tolerant module) |

> **Common GND is required.** Keep RESET on D2 and DC on D3. If you ever see boot problems, double‑check these two lines and your USB cable.

---

## 5) Centered Quote (two fonts)
This sketch clears the screen to black and renders a two‑line romantic quote exactly centered using two different fonts.

```cpp
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <Fonts/FreeSerifItalic9pt7b.h>  // line 1 (romantic/italic)
#include <Fonts/FreeSansBold9pt7b.h>     // line 2 (bold/clean)

#define TFT_CS  D1
#define TFT_DC  D3
#define TFT_RST D2

Adafruit_ST7735 tft(TFT_CS, TFT_DC, TFT_RST);

// Draw one centered line using the current GFX font
void drawCenteredLine(const char* text, int16_t centerY) {
  int16_t x1, y1; uint16_t w, h;
  tft.getTextBounds(text, 0, 0, &x1, &y1, &w, &h);
  int16_t x = (tft.width()  - (int)w) / 2;
  int16_t topY = centerY - (int)h / 2;
  int16_t baselineY = topY - y1;     // convert to baseline

  tft.setCursor(x, baselineY);
  tft.print(text);
}

void drawCenteredQuote(const char* l1, const char* l2) {
  tft.fillScreen(ST77XX_BLACK);
  tft.setTextWrap(false);
  tft.setTextColor(ST77XX_WHITE);

  // Measure both lines to center the pair vertically
  int16_t x1, y1, x2, y2; uint16_t w1, h1, w2, h2;

  tft.setFont(&FreeSerifItalic9pt7b);
  tft.getTextBounds(l1, 0, 0, &x1, &y1, &w1, &h1);

  tft.setFont(&FreeSansBold9pt7b);
  tft.getTextBounds(l2, 0, 0, &x2, &y2, &w2, &h2);

  const int lineGap = 4;
  const int totalH = (int)h1 + lineGap + (int)h2;
  const int centerY = tft.height() / 2;

  // Line 1 (italic)
  tft.setFont(&FreeSerifItalic9pt7b);
  drawCenteredLine(l1, centerY - (totalH/2) + h1/2);

  // Line 2 (bold)
  tft.setFont(&FreeSansBold9pt7b);
  drawCenteredLine(l2, centerY + (totalH/2) - h2/2);
}

void setup() {
  SPI.begin();
  SPI.setFrequency(8000000);      // 8 MHz is safe on ESP8266
  tft.initR(INITR_144GREENTAB);   // 1.44" 128x128 ST7735 preset
  tft.setRotation(0);             // try 0..3 if orientation differs

  // Change the quote text as you like:
  // Example:
  drawCenteredQuote("in your eyes,", "I find home");
}

void loop() {}
```

**Font tips:** On 128×128, 9pt is usually readable. If your quote is long, switch to the 7pt variants (e.g., `FreeSerifItalic7pt`, `FreeSansBold7pt`).

---

## 6) Optional: Show a color image (from microSD as BMP)
If you want multiple images without recompiling, add a microSD module to the same SPI bus.

**Additional wiring for SD module:**
- **SD_CS → D8** *(use D0 if D8 causes boot issues on your board)*
- **MISO → D6**
- MOSI → **D7** (shared with TFT)
- SCK → **D5** (shared with TFT)
- 3.3V and GND

**Code using Adafruit ImageReader (BMP, 24‑bit, uncompressed):**
```cpp
#include <SPI.h>
#include <SD.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <Adafruit_ImageReader.h>

#define TFT_CS  D1
#define TFT_DC  D3
#define TFT_RST D2
#define SD_CS   D8   // or D0

Adafruit_ST7735 tft(TFT_CS, TFT_DC, TFT_RST);
Adafruit_ImageReader reader;
ImageReturnCode stat;

void setup() {
  SPI.begin();
  SPI.setFrequency(8000000);
  tft.initR(INITR_144GREENTAB);
  tft.setRotation(0);
  tft.fillScreen(ST77XX_BLACK);

  if (!SD.begin(SD_CS)) {
    tft.setCursor(0,0); tft.setTextColor(ST77XX_RED); tft.print("SD FAIL");
    return;
  }

  // Place a 24-bit, uncompressed 128×128 BMP on the SD as /pic.bmp
  stat = reader.drawBMP("/pic.bmp", tft, 0, 0);
  if (stat != IMAGE_SUCCESS) {
    tft.setCursor(0,0); tft.setTextColor(ST77XX_RED);
    tft.print("BMP ERR "); tft.print((int)stat);
  }
}

void loop() {}
```

---

## 7) Troubleshooting
- **White screen:** Check CS/DC/RESET wiring; try SPI at **5 MHz**; try `setRotation(0..3)`.
- **Bottom noise/partial fill:** Ensure you are using **Adafruit_ST7735** with `initR(INITR_144GREENTAB)`.
- **Weird colors or orientation:** Try different `setRotation` values.
- **ESP8266 boot issues:** Keep RESET on **D2**, DC on **D3**; verify common GND; use a reliable USB data cable.

---

## 8) Notes
- For a single static image without SD, you can convert an image to a 128×128 **RGB565 PROGMEM** array and draw it with `drawRGBBitmap` per scanline. This avoids adding an SD module, but increases flash usage (~32 KB per 128×128 image).
- Keep jumper wires short (≤10–15 cm) for stable SPI signaling.
- Backlight at **3.3V** is recommended to avoid excess heat and brightness.

---

Happy building! 🎯
